package com.ruoyi.shenji.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
@Slf4j
public class FileUtils {

//    private  String excelFilePath = "D:\\QQFile\\RuoYi-Vue\\filemodels\\";
public void importStringListToExcel(HttpServletResponse response, String sheetName, List<String> dataList) {
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");

    try (XSSFWorkbook wb = new XSSFWorkbook()) {
        Sheet sheet = wb.createSheet(sheetName);

        int calNum = 0;
        Row row = sheet.createRow(0);
        for (String data : dataList) {
            Cell cell = row.createCell(calNum++);
            cell.setCellValue(data);
        }

        writeSheet(wb, sheet,sheetName);

        try (OutputStream outputStream = response.getOutputStream()) {
            wb.write(outputStream);
        }
    } catch (IOException e) {
        log.error("导出Excel异常：{}", e.getMessage());
    }
}

    private void writeSheet(XSSFWorkbook wb, Sheet sheet, String sheetName) {
        // 设置默认列宽（可选）
        sheet.setDefaultColumnWidth(20);

        // 添加表头（如果需要）
        if (sheetName.equals("用户数据")) { // 示例：仅当是“用户数据”时添加表头
            Row headerRow = sheet.createRow(0);
            Cell headerCell = headerRow.createCell(0);
            headerCell.setCellValue("字符串值");

            // 设置样式（如字体加粗、背景色等）（可选，这里仅示例）
            CellStyle style = wb.createCellStyle();
            Font font = wb.createFont();
            font.setBold(true);
            style.setFont(font);
            headerCell.setCellStyle(style);
        }
    }



}
